package com.zw.se2.general;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class MqttSubscribe {

    public String HOST = "tcp://112.230.206.158:8083";
    public String TOPIC = "DataExpress/TOPIC";
    private String clientid = "client";
    public MqttClient client;
    private MqttConnectOptions options;
    private String userName = "DataExpress";
    private String passWord = "DataExpress";
    private MqttCallback callback;

    private ScheduledExecutorService scheduler;

    public MqttSubscribe(String host, String clientid, MqttCallback callback) {
        this.HOST = "tcp://" + host + ":1883";
        this.clientid = clientid;
        this.callback = callback;
    }


    //��������
    public void startReconnect() {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(new Runnable() {
            public void run() {
                if (!client.isConnected()) {
                    try {
                        client.connect(options);
                    } catch (MqttSecurityException e) {
                        e.printStackTrace();
                    } catch (MqttException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, 0 * 1000, 10 * 1000, TimeUnit.MILLISECONDS);
    }

    public void start() {
        try {
            // hostΪ��������testΪclientid������MQTT�Ŀͻ���ID��һ���Կͻ���Ψһ��ʶ����ʾ��MemoryPersistence����clientid�ı�����ʽ��Ĭ��Ϊ���ڴ汣��
            client = new MqttClient(HOST, clientid, new MemoryPersistence());
            // MQTT����������
            options = new MqttConnectOptions();
            // �����Ƿ����session,�����������Ϊfalse��ʾ�������ᱣ���ͻ��˵����Ӽ�¼����������Ϊtrue��ʾÿ�����ӵ������������µ��������
            options.setCleanSession(true);
            // �������ӵ��û���
            options.setUserName(userName);
            // �������ӵ�����
            options.setPassword(passWord.toCharArray());
            // ���ó�ʱʱ�� ��λΪ��
            options.setConnectionTimeout(10);
            // ���ûỰ����ʱ�� ��λΪ�� ��������ÿ��1.5*20���ʱ����ͻ��˷��͸���Ϣ�жϿͻ����Ƿ����ߣ������������û�������Ļ���
            options.setKeepAliveInterval(15);
            // ���ûص�
            client.setCallback(callback);
//            MqttTopic topic = client.getTopic(TOPIC);
            //setWill�����������Ŀ����Ҫ֪���ͻ����Ƿ���߿��Ե��ø÷������������ն˿ڵ�֪ͨ��Ϣ
//            options.setWill(topic, "close".getBytes(), 0, true);

            client.connect(options);
            //������Ϣ
//            int[] Qos  = {1};
//            String[] topic1 = {TOPIC};
//            client.subscribe(topic1, Qos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void disconnect() {
        try {
            client.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void subscribe(String topic) throws MqttException {
        client.subscribe(topic, 1);
    }


}
